package com.fingard.dsp.bank.directbank.bos01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ITransaction;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7515TodayTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7516HisTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.RetAtsTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.detail.RetAtsTransDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Date;

public class BOS01Transaction extends BOS01Base implements ITransaction {

    @Override
    public RetAtsTrans d_7516_HisTrans(Req7516HisTrans p_req7516) throws Exception {
        RetAtsTrans retAts;
        if (getBankConfig().getValueAsBool("queryHisDetails1_1Op")) {
            retAts = d_7516_HisDetailsTrans(p_req7516);
        } else {
            retAts = d_7516_HisCurListTrans(p_req7516);
        }
        return retAts;
    }
    @Override
    public RetAtsTrans d_7515_TodayTrans(Req7515TodayTrans p_req7515) throws Exception {
        RetAtsTrans retAts;
        if (getBankConfig().getValueAsBool("queryDayDetails1_1Op")) {
            retAts = d_7515_TodayDetTrans(p_req7515);
        } else {
            retAts = d_7515_TodayCurTrans(p_req7515);
        }
        return retAts;
    }
	/**
	 * opName：查询活期历史明细（方正版）“queryHistoryCurrentListNEW1_1Op”
	 */
    public RetAtsTrans d_7516_HisCurListTrans(Req7516HisTrans p_req7516) throws Exception {
        RetAtsTrans retAts = new RetAtsTrans(p_req7516.reqHeader);
        retAts.writeStart();
        
        String tmpRespCode = "";
        String tmpRespInfo = "";
        
        try {
            boolean jionforeignAct = getBankConfig().getValueAsBool("jionforeignAct");
            WriteBankLogLn("jionforeignAct:" + jionforeignAct);
            if (jionforeignAct) {
                String foreignActMapping = getBankConfig().getMapValueByKey("foreignActMapping", p_req7516.actNum);
                WriteBankLogLn("foreignActMapping:" + "[key:" + p_req7516.actNum + ",value:" + foreignActMapping + "]");
                p_req7516.actNum = StringHelper.joinNotEmpty("", foreignActMapping, p_req7516.actNum);
            }

            BOS01Header tmpHeader = new BOS01Header();
            tmpHeader.opName = "queryHistoryCurrentListNEW1_1Op";
            tmpHeader.serialNo = getQryTransID();
            tmpHeader.needSigned = false;

            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, tmpHeader);
            xmlWriter.writeElementString("ACNO", p_req7516.actNum);
            xmlWriter.writeElementString("QSRQ", p_req7516.dateFrom);
            xmlWriter.writeElementString("ZZRQ", p_req7516.dateTo);
            xmlWriter.writeElementString("MIFS", "0");
            xmlWriter.writeElementString("MAFS", "99999999999999999.99");
            xmlWriter.writeEndDocument();
            
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), tmpHeader);
            
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmprspcod = tmpBkReader.getFirstText("/BOSEBankData/opRep/retCode");
                    String tmprspmsg = tmpBkReader.getFirstText("/BOSEBankData/opRep/errMsg");
                    if ("0".equals(tmprspcod)) {
                        while (tmpBkReader.read("/BOSEBankData/opRep/opResultSet/opResult")) {
                        	RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
                            XElement tmpRecord = tmpBkReader.readedXElement;
                            
                            tmpRetDetail.cdFlag = tmpRecord.getSingleText("CDFG");
                            tmpRetDetail.tranAmt = Format.DecimalAmtFormat.format(Double.parseDouble(tmpRecord.getSingleText("FSJE").replace(",", "")));
                            String tmpTranDate = tmpRecord.getSingleText("JYRQ");
                            tmpRetDetail.tranDate = tmpTranDate.substring(0, 4) + "-" + tmpTranDate.substring(5, 7) + "-" + tmpTranDate.substring(8, 10);
                            tmpRetDetail.valueDate = tmpRetDetail.tranDate;
                            tmpRetDetail.tranTime = tmpRetDetail.tranDate + " " + tmpRecord.getSingleText("FSSJ") + ":00";
                            tmpRetDetail.postTimestamp = tmpRetDetail.tranTime;
                            tmpRetDetail.recAccNum = tmpRecord.getSingleText("DFZH");
                            tmpRetDetail.recAccName = tmpRecord.getSingleText("DFHM");
                            tmpRetDetail.billType = tmpRecord.getSingleText("VCTP");
                            tmpRetDetail.billNum = tmpRecord.getSingleText("VCNO");
                            tmpRetDetail.accBal = Format.DecimalAmtFormat.format(Double.parseDouble(tmpRecord.getSingleText("YUER").replace(",", "")));
                            tmpRetDetail.usage = tmpRecord.getSingleText("YOTU");
                            tmpRetDetail.abstr = pickupAbstr(tmpRecord.getSingleText("YOTU"), tmpRecord.getSingleText("BEZH"));
                            tmpRetDetail.comment = tmpRecord.getSingleText("BEZH");
                            tmpRetDetail.urid = tmpRecord.getSingleText("T24F");
                            
                            retAts.writeRetDetail(tmpRetDetail);
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmprspmsg;
                    }
                    if (tmprspmsg.contains("非法SESSION") || tmprspmsg.contains("SESSION已超时")) {
                        bankFront.token = "";
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
        	ex.printStackTrace();
            tmpRespCode = "-1";
            tmpRespInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }
        
        if ("-1".equals(tmpRespCode)) {
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("交易为空");
        }
        
        retAts.writeEnd();
        return retAts;
    }

    /**
     * opName：查询活期历史明细（显示隔日抹账）“queryHisDetails1_1Op”
     */
    public RetAtsTrans d_7516_HisDetailsTrans(Req7516HisTrans p_req7516) throws Exception {
        RetAtsTrans retAts = new RetAtsTrans(p_req7516.reqHeader);
        retAts.writeStart();

        String tmpRespCode = "";
        String tmpRespInfo = "";

        try {
            boolean jionforeignAct = getBankConfig().getValueAsBool("jionforeignAct");
            WriteBankLogLn("jionforeignAct:" + jionforeignAct);
            if (jionforeignAct) {
                String foreignActMapping = getBankConfig().getMapValueByKey("foreignActMapping", p_req7516.actNum);
                WriteBankLogLn("foreignActMapping:" + "[key:" + p_req7516.actNum + ",value:" + foreignActMapping + "]");
                p_req7516.actNum = StringHelper.joinNotEmpty("", foreignActMapping, p_req7516.actNum);
            }

            BOS01Header tmpHeader = new BOS01Header();
            tmpHeader.opName = "queryHisDetails1_1Op";
            tmpHeader.serialNo = getQryTransID();
            tmpHeader.needSigned = false;

            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, tmpHeader);
            xmlWriter.writeElementString("ACNO", p_req7516.actNum);
            xmlWriter.writeElementString("QSRQ", p_req7516.dateFrom);
            xmlWriter.writeElementString("ZZRQ", p_req7516.dateTo);
            xmlWriter.writeElementString("MIFS", "0");
            xmlWriter.writeElementString("MAFS", "99999999999999999.99");
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), tmpHeader);

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmprspcod = tmpBkReader.getFirstText("/BOSEBankData/opRep/retCode");
                    String tmprspmsg = tmpBkReader.getFirstText("/BOSEBankData/opRep/errMsg");
                    if ("0".equals(tmprspcod)) {
                        while (tmpBkReader.read("/BOSEBankData/opRep/opResultSet/opResult")) {
                            RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
                            XElement tmpRecord = tmpBkReader.readedXElement;

                            tmpRetDetail.cdFlag = tmpRecord.getSingleText("CDFG");
                            tmpRetDetail.usage = tmpRecord.getSingleText("PATI");
                            tmpRetDetail.tranAmt = Format.DecimalAmtFormat.format(Double.parseDouble(tmpRecord.getSingleText("FSJE").replace(",", "")));
                            String tmpTranDate = tmpRecord.getSingleText("JYRQ");
                            tmpRetDetail.tranDate = tmpTranDate.substring(0, 4) + "-" + tmpTranDate.substring(5, 7) + "-" + tmpTranDate.substring(8, 10);
                            tmpRetDetail.valueDate = tmpRetDetail.tranDate;
                            tmpRetDetail.tranTime = tmpRetDetail.tranDate + " " + tmpRecord.getSingleText("FSSJ") + ":00";
                            tmpRetDetail.postTimestamp = tmpRetDetail.tranTime;
                            tmpRetDetail.recAccNum = tmpRecord.getSingleText("DFZH");
                            tmpRetDetail.recAccName = tmpRecord.getSingleText("DFHM");
                            tmpRetDetail.billType = tmpRecord.getSingleText("VCTP");
                            tmpRetDetail.billNum = tmpRecord.getSingleText("VCNO");
                            tmpRetDetail.accBal = Format.DecimalAmtFormat.format(Double.parseDouble(tmpRecord.getSingleText("YUER").replace(",", "")));
                            tmpRetDetail.usage = tmpRecord.getSingleText("YOTU");
                            tmpRetDetail.abstr = pickupAbstr(tmpRecord.getSingleText("YOTU"), tmpRecord.getSingleText("BEZH"));
                            tmpRetDetail.comment = tmpRecord.getSingleText("BEZH");
                            tmpRetDetail.urid = tmpRecord.getSingleText("T24F");

                            retAts.writeRetDetail(tmpRetDetail);
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmprspmsg;
                    }
                    if (tmprspmsg.contains("非法SESSION") || tmprspmsg.contains("SESSION已超时")) {
                        bankFront.token = "";
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRespCode = "-1";
            tmpRespInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        if ("-1".equals(tmpRespCode)) {
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("交易为空");
        }

        retAts.writeEnd();
        return retAts;
    }

    /**
     * opName：查询活期当日明细（方正版）“queryDailyCurrentListNEW1_1Op”
     */
    public RetAtsTrans d_7515_TodayCurTrans(Req7515TodayTrans p_req7515) throws Exception {
        RetAtsTrans retAts = new RetAtsTrans(p_req7515.reqHeader);
        retAts.writeStart();

        String tmpRespCode = "";
        String tmpRespInfo = "";

        try {
            boolean jionforeignAct = getBankConfig().getValueAsBool("jionforeignAct");
            WriteBankLogLn("jionforeignAct:" + jionforeignAct);
            if (jionforeignAct) {
                String foreignActMapping = getBankConfig().getMapValueByKey("foreignActMapping", p_req7515.actNum);
                WriteBankLogLn("foreignActMapping:" + "[key:" + p_req7515.actNum + ",value:" + foreignActMapping + "]");
                p_req7515.actNum = StringHelper.joinNotEmpty("", foreignActMapping, p_req7515.actNum);
            }

            BOS01Header tmpHeader = new BOS01Header();
            tmpHeader.opName = "queryDailyCurrentListNEW1_1Op";
            tmpHeader.serialNo = getQryTransID();
            tmpHeader.needSigned = false;

            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, tmpHeader);

            xmlWriter.writeElementString("ACNO", p_req7515.actNum);
            xmlWriter.writeElementString("MIFS", "0");
            xmlWriter.writeElementString("MAFS", "99999999999999999.99");
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), tmpHeader);

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmprspcod = tmpBkReader.getFirstText("/BOSEBankData/opRep/retCode");
                    String tmprspmsg = tmpBkReader.getFirstText("/BOSEBankData/opRep/errMsg");
                    if ("0".equals(tmprspcod)) {
                        while (tmpBkReader.read("/BOSEBankData/opRep/opResultSet/opResult")) {
                        	RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
                            XElement tmpRecord = tmpBkReader.readedXElement;

                            tmpRetDetail.cdFlag = tmpRecord.getSingleText("CDFG");
                            tmpRetDetail.tranAmt = Format.DecimalAmtFormat.format(Double.parseDouble(tmpRecord.getSingleText("FSJE").replace(",", "")));
                            tmpRetDetail.tranDate = Format.Date10Format.format(new Date());
                            tmpRetDetail.valueDate = tmpRetDetail.tranDate;
                            tmpRetDetail.tranTime = tmpRetDetail.tranDate + " " + tmpRecord.getSingleText("FSSJ") + ":00";
                            tmpRetDetail.postTimestamp = tmpRetDetail.tranTime;
                            tmpRetDetail.recAccNum = tmpRecord.getSingleText("DFZH");
                            tmpRetDetail.recAccName = tmpRecord.getSingleText("DFHM");
                            tmpRetDetail.billType = tmpRecord.getSingleText("VCTP");
                            tmpRetDetail.billNum = tmpRecord.getSingleText("VCNO");
                            tmpRetDetail.accBal = Format.DecimalAmtFormat.format(Double.parseDouble(tmpRecord.getSingleText("YUER").replace(",", "")));
                            tmpRetDetail.usage = tmpRecord.getSingleText("YOTU");
                            tmpRetDetail.abstr = pickupAbstr(tmpRecord.getSingleText("YOTU"), tmpRecord.getSingleText("BEZH"));
                            tmpRetDetail.comment = tmpRecord.getSingleText("BEZH");
                            tmpRetDetail.urid = tmpRecord.getSingleText("T24F");

                            retAts.writeRetDetail(tmpRetDetail);
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmprspmsg;
                    }
                    if (tmprspmsg.contains("非法SESSION") || tmprspmsg.contains("SESSION已超时")) {
                        bankFront.token = "";
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
        	ex.printStackTrace();
            tmpRespCode = "-1";
            tmpRespInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        if ("-1".equals(tmpRespCode)) {
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("交易为空");
        }

        retAts.writeEnd();
        return retAts;
    }

    /**
     * opName：查询活期当日明细（显示隔日抹账）“queryDayDetails1_1Op”
     */
    public RetAtsTrans d_7515_TodayDetTrans(Req7515TodayTrans p_req7515) throws Exception {
        RetAtsTrans retAts = new RetAtsTrans(p_req7515.reqHeader);
        retAts.writeStart();

        String tmpRespCode = "";
        String tmpRespInfo = "";

        try {
            boolean jionforeignAct = getBankConfig().getValueAsBool("jionforeignAct");
            WriteBankLogLn("jionforeignAct:" + jionforeignAct);
            if (jionforeignAct) {
                String foreignActMapping = getBankConfig().getMapValueByKey("foreignActMapping", p_req7515.actNum);
                WriteBankLogLn("foreignActMapping:" + "[key:" + p_req7515.actNum + ",value:" + foreignActMapping + "]");
                p_req7515.actNum = StringHelper.joinNotEmpty("", foreignActMapping, p_req7515.actNum);
            }

            BOS01Header tmpHeader = new BOS01Header();
            tmpHeader.opName = "queryDayDetails1_1Op";
            tmpHeader.serialNo = getQryTransID();
            tmpHeader.needSigned = false;

            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, tmpHeader);

            xmlWriter.writeElementString("ACNO", p_req7515.actNum);
            xmlWriter.writeElementString("MIFS", "0");
            xmlWriter.writeElementString("MAFS", "99999999999999999.99");
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), tmpHeader);

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmprspcod = tmpBkReader.getFirstText("/BOSEBankData/opRep/retCode");
                    String tmprspmsg = tmpBkReader.getFirstText("/BOSEBankData/opRep/errMsg");
                    if ("0".equals(tmprspcod)) {
                        while (tmpBkReader.read("/BOSEBankData/opRep/opResultSet/opResult")) {
                            RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
                            XElement tmpRecord = tmpBkReader.readedXElement;

                            tmpRetDetail.cdFlag = tmpRecord.getSingleText("CDFG");
                            tmpRetDetail.usage = tmpRecord.getSingleText("PATI");
                            tmpRetDetail.tranAmt = Format.DecimalAmtFormat.format(Double.parseDouble(tmpRecord.getSingleText("FSJE").replace(",", "")));
                            tmpRetDetail.tranDate = Format.Date10Format.format(new Date());
                            tmpRetDetail.valueDate = tmpRetDetail.tranDate;
                            tmpRetDetail.tranTime = tmpRetDetail.tranDate + " " + tmpRecord.getSingleText("FSSJ") + ":00";
                            tmpRetDetail.postTimestamp = tmpRetDetail.tranTime;
                            tmpRetDetail.recAccNum = tmpRecord.getSingleText("DFZH");
                            tmpRetDetail.recAccName = tmpRecord.getSingleText("DFHM");
                            tmpRetDetail.billType = tmpRecord.getSingleText("VCTP");
                            tmpRetDetail.billNum = tmpRecord.getSingleText("VCNO");
                            tmpRetDetail.accBal = Format.DecimalAmtFormat.format(Double.parseDouble(tmpRecord.getSingleText("YUER").replace(",", "")));
                            tmpRetDetail.usage = tmpRecord.getSingleText("YOTU");
                            tmpRetDetail.abstr = pickupAbstr(tmpRecord.getSingleText("YOTU"), tmpRecord.getSingleText("BEZH"));
                            tmpRetDetail.comment = tmpRecord.getSingleText("BEZH");
                            tmpRetDetail.urid = tmpRecord.getSingleText("T24F");

                            retAts.writeRetDetail(tmpRetDetail);
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmprspmsg;
                    }
                    if (tmprspmsg.contains("非法SESSION") || tmprspmsg.contains("SESSION已超时")) {
                        bankFront.token = "";
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRespCode = "-1";
            tmpRespInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        if ("-1".equals(tmpRespCode)) {
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("交易为空");
        }

        retAts.writeEnd();
        return retAts;
    }


}
